{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Predicting delayed flights with classification analysis\n",
    "\n",
    "[This is a companion Jupyter notebook to the documentation example.](https://www.elastic.co/guide/en/elastic-stack-overview/7.5/flightdata-classification.html)\n",
    "\n",
    "Let’s try to predict whether a flight will be delayed or not by using the sample flight data. We want to be able to use information such as weather conditions, carrier, flight distance, origin, or destination to predict flight delays. There are only two possible outcome values: the flight is either delayed or not, therefore we use binary classification to make the prediction.\n",
    "\n",
    "We have chosen this dataset as an example because it is easily accessible for Kibana users and the use case is relevant. However, the data has been manually created and contains some inconsistencies. For example, a flight can be both delayed and canceled. Please remember that the quality of your input data will affect the quality of results.\n",
    "\n",
    "Each document in the dataset contains details for a single flight, so this data is ready for analysis as it is already in a two-dimensional entity-based data structure (data frame). In general, you often need to transform the data into an entity-centric index before you analyze the data.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "## imports\n",
    "import pprint\n",
    "\n",
    "from elasticsearch import Elasticsearch\n",
    "import requests\n",
    "## create a client to connect to Elasticsearch\n",
    "es_url = 'http://localhost:9200'\n",
    "es_client = Elasticsearch()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Example Document"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'hits': {'hits': [{'_index': 'kibana_sample_data_flights',\n",
       "    '_id': 'HZL81W4BszKtAdTQ9e-h',\n",
       "    '_score': 1.0,\n",
       "    '_source': {'FlightNum': '9HY9SWR',\n",
       "     'DestCountry': 'AU',\n",
       "     'OriginWeather': 'Sunny',\n",
       "     'OriginCityName': 'Frankfurt am Main',\n",
       "     'AvgTicketPrice': 841.2656419677076,\n",
       "     'DistanceMiles': 10247.856675613455,\n",
       "     'FlightDelay': False,\n",
       "     'DestWeather': 'Rain',\n",
       "     'Dest': 'Sydney Kingsford Smith International Airport',\n",
       "     'FlightDelayType': 'No Delay',\n",
       "     'OriginCountry': 'DE',\n",
       "     'dayOfWeek': 0,\n",
       "     'DistanceKilometers': 16492.32665375846,\n",
       "     'timestamp': '2019-11-25T00:00:00',\n",
       "     'DestLocation': {'lat': '-33.94609833', 'lon': '151.177002'},\n",
       "     'DestAirportID': 'SYD',\n",
       "     'Carrier': 'Kibana Airlines',\n",
       "     'Cancelled': False,\n",
       "     'FlightTimeMin': 1030.7704158599038,\n",
       "     'Origin': 'Frankfurt am Main Airport',\n",
       "     'OriginLocation': {'lat': '50.033333', 'lon': '8.570556'},\n",
       "     'DestRegion': 'SE-BD',\n",
       "     'OriginAirportID': 'FRA',\n",
       "     'OriginRegion': 'DE-HE',\n",
       "     'DestCityName': 'Sydney',\n",
       "     'FlightTimeHour': 17.179506930998397,\n",
       "     'FlightDelayMin': 0}}]}}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## insert example of reading docs from ES index\n",
    "\n",
    "results = es_client.search(index='kibana_sample_data_flights', filter_path=['hits.hits._*'], size=1)\n",
    "results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that each document contains a FlightDelay field with a boolean value. Classification is a supervised machine learning analysis and therefore needs to train on data that contains the ground truth, known as the dependent_variable. In this example, the ground truth is available in each document as the actual value of FlightDelay. In order to be analyzed, a document must contain at least one field with a supported data type (numeric, boolean, text, keyword or ip) and must not contain arrays with more than one item.\n",
    "\n",
    "If your source data consists of some documents that contain a dependent variable and some that do not, the model is trained on the subset of documents that contain ground truth. By default, all of that subset of documents is used for training. However, you can choose to specify a percentage of the documents as your training data. Predictions are made against all of the data. The current implementation of classification analysis supports a single batch analysis for both training and predictions."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Creating a Classification Model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'allow_lazy_start': False,\n",
      " 'analysis': {'classification': {'dependent_variable': 'FlightDelay',\n",
      "                                 'num_top_classes': 2,\n",
      "                                 'prediction_field_name': 'FlightDelay_prediction',\n",
      "                                 'training_percent': 10.0}},\n",
      " 'analyzed_fields': {'excludes': ['Cancelled',\n",
      "                                  'FlightDelayMin',\n",
      "                                  'FlightDelayType'],\n",
      "                     'includes': []},\n",
      " 'create_time': 1576584651508,\n",
      " 'dest': {'index': 'df-flight-delayed', 'results_field': 'ml'},\n",
      " 'id': 'model-flight-delay-classification',\n",
      " 'model_memory_limit': '100mb',\n",
      " 'source': {'index': ['kibana_sample_data_flights'],\n",
      "            'query': {'match_all': {}}},\n",
      " 'version': '8.0.0'}\n"
     ]
    }
   ],
   "source": [
    "# 1. Creating a classification job \n",
    "\n",
    "endpoint_url = \"/_ml/data_frame/analytics/model-flight-delay-classification\"\n",
    "\n",
    "job_config = {\n",
    "  \"source\": {\n",
    "    \"index\": [\n",
    "      \"kibana_sample_data_flights\"  \n",
    "    ]\n",
    "  },\n",
    "  \"dest\": {\n",
    "    \"index\": \"df-flight-delayed\",  \n",
    "    \"results_field\": \"ml\" \n",
    "  },\n",
    "  \"analysis\": {\n",
    "    \"classification\": {\n",
    "      \"dependent_variable\": \"FlightDelay\",  \n",
    "      \"training_percent\": 10  # see comment below on training percent\n",
    "    }\n",
    "  },\n",
    "  \"analyzed_fields\": {\n",
    "    \"includes\": [],\n",
    "    \"excludes\": [    \n",
    "      \"Cancelled\",\n",
    "      \"FlightDelayMin\",\n",
    "      \"FlightDelayType\"\n",
    "    ]\n",
    "  },\n",
    "  \"model_memory_limit\": \"100mb\"}\n",
    "\n",
    "result = requests.put(es_url+endpoint_url, json=job_config)\n",
    "pprint.pprint(result.json())\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### A brief note on training percentage\n",
    "\n",
    "As you may have noticed, in the job configuration above we set the value of `training_percent` to 10. This means that out of the whole Flights dataset 10 percent of the data will be used to train model and the remaining 90 percent of the data will be used for testing the model. \n",
    "You might wonder at this point, what is the best percentage for the train/test split and how you should choose what percentage to use in your own job? The answer will usually depend on your particular situation. In general it is useful to consider some of the following tradeoffs.\n",
    "The more data you supply to the model at training time, the more examples the model will have to learn from, which usually leads to a better classification performance. However, more training data will also increase the training time of the model and at some point, providing the model with more training examples will only result in marginal increase in accuracy. \n",
    "\n",
    "Moreover, the more data you use for training, the less data you have for the testing phase. This means that you will have less previously unseen examples to show your model and thus perhaps your estimate for the generalization error will not be as accurate. \n",
    "\n",
    "In general, for datasets containing several thousand docs or more, start with a low 5-10% training percentage and see how your results and runtime evolve as you increase the training percentage. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'acknowledged': True}\n"
     ]
    }
   ],
   "source": [
    "# 2. Start the job\n",
    "\n",
    "start_endpoint = \"/_ml/data_frame/analytics/model-flight-delay-classification/_start\"\n",
    "result = requests.post(es_url+start_endpoint)\n",
    "pprint.pprint(result.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The job takes a few minutes to run. Runtime depends on the local hardware and also on the number of documents and fields that are analyzed. The more fields and documents, the longer the job runs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'count': 1,\n",
      " 'data_frame_analytics': [{'id': 'model-flight-delay-classification',\n",
      "                           'progress': [{'phase': 'reindexing',\n",
      "                                         'progress_percent': 100},\n",
      "                                        {'phase': 'loading_data',\n",
      "                                         'progress_percent': 100},\n",
      "                                        {'phase': 'analyzing',\n",
      "                                         'progress_percent': 100},\n",
      "                                        {'phase': 'writing_results',\n",
      "                                         'progress_percent': 100}],\n",
      "                           'state': 'stopped'}]}\n"
     ]
    }
   ],
   "source": [
    "# 3. Check the job stats\n",
    "\n",
    "stats_endpoint = \"/_ml/data_frame/analytics/model-flight-delay-classification/_stats\"\n",
    "result = requests.get(es_url+stats_endpoint)\n",
    "pprint.pprint(result.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## View Classification Results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'hits': {'hits': [{'_index': 'df-flight-delayed',\n",
       "    '_id': '-5L81W4BszKtAdTQ-Pdn',\n",
       "    '_score': 0.10778817,\n",
       "    '_source': {'FlightNum': 'YHH7FJ3',\n",
       "     'Origin': 'Rochester International Airport',\n",
       "     'OriginLocation': {'lon': '-92.5', 'lat': '43.90829849'},\n",
       "     'DestLocation': {'lon': '-117.5339966', 'lat': '47.61989975'},\n",
       "     'FlightDelay': False,\n",
       "     'DistanceMiles': 1231.1973824768306,\n",
       "     'FlightTimeMin': 94.35333906213299,\n",
       "     'OriginWeather': 'Clear',\n",
       "     'dayOfWeek': 0,\n",
       "     'AvgTicketPrice': 126.52980202058899,\n",
       "     'Carrier': 'Logstash Airways',\n",
       "     'FlightDelayMin': 0,\n",
       "     'OriginRegion': 'US-MN',\n",
       "     'FlightDelayType': 'No Delay',\n",
       "     'DestAirportID': 'GEG',\n",
       "     'timestamp': '2019-12-02T12:16:06',\n",
       "     'Dest': 'Spokane International Airport',\n",
       "     'FlightTimeHour': 1.5725556510355498,\n",
       "     'Cancelled': False,\n",
       "     'DistanceKilometers': 1981.4201203047928,\n",
       "     'OriginCityName': 'Rochester',\n",
       "     'DestWeather': 'Cloudy',\n",
       "     'OriginCountry': 'US',\n",
       "     'ml__id_copy': '-5L81W4BszKtAdTQ-Pdn',\n",
       "     'DestCountry': 'US',\n",
       "     'DestRegion': 'US-WA',\n",
       "     'OriginAirportID': 'RST',\n",
       "     'DestCityName': 'Spokane',\n",
       "     'ml': {'top_classes': [{'class_probability': 0.9876257877232794,\n",
       "        'class_name': 'false'},\n",
       "       {'class_probability': 0.012374212276720642, 'class_name': 'true'}],\n",
       "      'FlightDelay_prediction': 'false',\n",
       "      'prediction_probability': 0.9876257877232794,\n",
       "      'is_training': False}}}]}}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# insert code to get results\n",
    "query = {\"query\": {\"term\": {\"ml.is_training\": {\"value\": False }}}}\n",
    "result = es_client.search(index='df-flight-delayed', filter_path=['hits.hits._*'], size=1, body=query)\n",
    "result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The example above shows that the analysis has predicted the probability of all possible classes. In this case, there are two classes: `true` and `false`. The class names along with the probability of the given classes are displayed in the top_classes object. The most probable class is the prediction. In the example above, `true` has a class_probability of 0.92 while `false` has only 0.08, so the prediction will be `true` which coincides with the ground truth contained by the FlightDelay field. The class probability values help you understand how sure the model is about the prediction. The higher number means that the model is more confident."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Evaluating Results\n",
    "The results can be evaluated for documents which contain both the ground truth field and the prediction. In the example below, FlightDelay contains the ground truth and the prediction is stored as FlightDelay_prediction.\n",
    "\n",
    "We use the data frame analytics evaluate API to evaluate the results. First, we want to know the training error that represents how well the model performed on the training dataset. In the previous step, we saw that the new index contained a field that indicated which documents were used as training data, which we can now use to calculate the training error:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'classification': {'multiclass_confusion_matrix': {'confusion_matrix': [{'actual_class': 'false',\n",
       "     'actual_class_doc_count': 1000,\n",
       "     'predicted_classes': [{'predicted_class': 'false', 'count': 904},\n",
       "      {'predicted_class': 'true', 'count': 96}],\n",
       "     'other_predicted_class_doc_count': 0},\n",
       "    {'actual_class': 'true',\n",
       "     'actual_class_doc_count': 334,\n",
       "     'predicted_classes': [{'predicted_class': 'false', 'count': 20},\n",
       "      {'predicted_class': 'true', 'count': 314}],\n",
       "     'other_predicted_class_doc_count': 0}],\n",
       "   'other_actual_class_count': 0}}}"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# compute the training error\n",
    "\n",
    "evaluate_endpoint = \"/_ml/data_frame/_evaluate\"\n",
    "\n",
    "config = {\n",
    " \"index\": \"df-flight-delayed\",  \n",
    "   \"query\": {\n",
    "    \"term\": {\n",
    "      \"ml.is_training\": {\n",
    "        \"value\": True  \n",
    "      }\n",
    "    }\n",
    "  },\n",
    " \"evaluation\": {\n",
    "   \"classification\": {\n",
    "     \"actual_field\": \"FlightDelay\",  \n",
    "     \"predicted_field\": \"ml.FlightDelay_prediction\",  \n",
    "     \"metrics\": {\n",
    "       \"multiclass_confusion_matrix\" : {}\n",
    "     }\n",
    "   }\n",
    " }\n",
    "}\n",
    "\n",
    "result = requests.post(es_url+evaluate_endpoint, json=config)\n",
    "result.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we calculate the generalization error that represents how well the model performed on previously unseen data. The returned confusion matrix shows us how many datapoints were classified correctly (where the actual_class matches the predicted_class) and how many were misclassified (actual_class does not match predicted_class):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'classification': {'multiclass_confusion_matrix': {'confusion_matrix': [{'actual_class': 'false',\n",
       "     'actual_class_doc_count': 8779,\n",
       "     'predicted_classes': [{'predicted_class': 'false', 'count': 7176},\n",
       "      {'predicted_class': 'true', 'count': 1603}],\n",
       "     'other_predicted_class_doc_count': 0},\n",
       "    {'actual_class': 'true',\n",
       "     'actual_class_doc_count': 2946,\n",
       "     'predicted_classes': [{'predicted_class': 'false', 'count': 963},\n",
       "      {'predicted_class': 'true', 'count': 1983}],\n",
       "     'other_predicted_class_doc_count': 0}],\n",
       "   'other_actual_class_count': 0}}}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# compute the generalization error\n",
    "\n",
    "config = {\n",
    " \"index\": \"df-flight-delayed\",  \n",
    "   \"query\": {\n",
    "    \"term\": {\n",
    "      \"ml.is_training\": {\n",
    "        \"value\": False\n",
    "      }\n",
    "    }\n",
    "  },\n",
    " \"evaluation\": {\n",
    "   \"classification\": {\n",
    "     \"actual_field\": \"FlightDelay\",  \n",
    "     \"predicted_field\": \"ml.FlightDelay_prediction\",  \n",
    "     \"metrics\": {\n",
    "       \"multiclass_confusion_matrix\" : {}\n",
    "     }\n",
    "   }\n",
    " }\n",
    "}\n",
    "\n",
    "result = requests.post(es_url+evaluate_endpoint, json=config)\n",
    "result.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
